package com.xinghanlaser.applet.base.flow.core;

import com.xinghanlaser.applet.base.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * <p>
 *
 * @author: mftuo 陀铭峰
 * @since: 2024/5/17 16:52
 */
@Component
public class PointHandleUtil{
    private final Map<Long, BasePoint> pointMap;

    @Resource
    private Validator validator;

    @Autowired
    public PointHandleUtil(List<BasePoint> baseServices) {
        // 初始化节点
        pointMap = baseServices.stream()
                .collect(Collectors.toMap(
                        BasePoint::getPointId,
                        item -> item)
                );
    }

    public void executeCurrentPoint(Map<String, Object> pointData, Long pointId){
        // 获取节点实例
        BasePoint pointEntity = pointMap.get(pointId);

        if(pointEntity == null){
            throw new ServiceException("节点ID不存在");
        }

        // 执行节点方法
        pointEntity.run(pointData, validator);

    }
}
